package me.zhengjie.modules.easycode.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import me.zhengjie.base.QueryHelpMybatisPlus;
import me.zhengjie.modules.easycode.domain.Project;
import me.zhengjie.modules.easycode.service.ProjectService;
import me.zhengjie.modules.easycode.service.dto.ProjectDto;
import me.zhengjie.modules.easycode.service.dto.ProjectQueryParam;
import me.zhengjie.modules.easycode.service.mapper.ProjectMapper;
import me.zhengjie.utils.ConvertUtil;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
 * @author guoqing
 * @date 2021-06-16
 */
@Service
@AllArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ProjectServiceImpl implements ProjectService {

    private final ProjectMapper projectMapper;


    @Override
    public synchronized Project createProject() {
        final Long currentUserId = SecurityUtils.getCurrentUserId();
        final Integer count = projectMapper.selectCount(new QueryWrapper<Project>().eq("userid", currentUserId));
        String projectCode = "A00" + (count + 1);
        final Project project = new Project();
        project.setUserid(currentUserId);
        project.setProjectCode(projectCode);
        project.setProjectName("新项目");
        final int insert = projectMapper.insert(project);
        if (insert > 0) {
            return project;
        } else {
            return null;
        }
    }


    @Override
    public List<ProjectDto> queryAll(ProjectQueryParam query) {
        return ConvertUtil.convertList(projectMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), ProjectDto.class);
    }

    @Override
    public Project getById(Integer id) {
        return projectMapper.selectById(id);
    }

    @Override
    public Project getByProjectCode(String projectCode) {
        if (StrUtil.isEmpty(projectCode)) {
            return null;
        }
        final Long userId = SecurityUtils.getCurrentUserId();
        final Project project = projectMapper.selectOne(new QueryWrapper<Project>().eq("project_code", projectCode).eq("userid", userId));
        return project;
    }

    @Override
    public ProjectDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), ProjectDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(ProjectDto resources) {
        Project entity = ConvertUtil.convert(resources, Project.class);
        return projectMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(ProjectDto resources) {
        Project entity = ConvertUtil.convert(resources, Project.class);
        int ret = projectMapper.updateById(entity);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids) {
        return projectMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id) {
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

}
